<cfsetting enablecfoutputonly="Yes">
<!---
	where/places.cfm
--->

<cfcontent type="application/xml">

<cftry>
	<cfset params=structNew()>
	<cfset paramList="cx,cy,n,e,w,s">
	<cfset MaxResults=25>
	<cfparam name="Form.pid" type="string" default="">
	<cfloop list="#paramList#" index="paramName">
		<cfif not structKeyExists(Form, paramName)><cfthrow message="Missing parameter #paramName#"></cfif>
		<cfset v=Form[paramName]>
		<cfif not isNumeric(v)><cfthrow message="Invalid parameter #paramName#"></cfif>
		<cfset v=val(v)>
		<cfif (("cxwe" CONTAINS paramName) AND ((v lt -180) or (v gt 180))) or (("cyns" CONTAINS paramName) AND ((v lt -90) or (v gt 90)))>
			<cfthrow message="Invalid data in #paramName#">
		</cfif>
		<cfset params[paramName]=int(v*1000000)>
	</cfloop>
	<cfif params.n lt params.s><cfset t=params.n><cfset params.n=params.s><cfset params.s=t></cfif>
	<cfif params.w lt params.e><cfset t=params.w><cfset params.w=params.e><cfset params.e=t></cfif>
	<cfquery datasource="nowrists2007" name="LocalPlaces">
	SELECT TOP #MaxResults#
	  ABS(a.cx - p.lng) + ABS(a.cy - p.lat) AS diff,
	  p.id AS placeId,
	  CONVERT(DECIMAL(9,6),CONVERT(DECIMAL(15,6),p.lat) * 0.000001) AS lat,
	  CONVERT(DECIMAL(9,6),CONVERT(DECIMAL(15,6),p.lng) * 0.000001) AS lng,
	  p.title,
	  p.street, p.city, p.region, p.country, p.phone
	FROM (
	    SELECT <cfloop list="#paramList#" index="paramName"><cfif paramName neq listFirst(paramList)>, </cfif>
		  <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#params[paramName]#"> AS #paramName#</cfloop>
	  ) AS a
	  INNER JOIN nws_places AS p ON (p.lat BETWEEN a.s AND a.n) AND (p.lng BETWEEN a.e AND a.w)
	ORDER BY 1
	</cfquery>
	<cfoutput><places key="#Session.GeoKey#" pid="#XMLFormat(Form.pid)#"></cfoutput>
	<cfoutput query="LocalPlaces"><place<cfloop list="#LocalPlaces.columnList#" index="colName"><cfif colName neq "diff"> #lCase(colName)#="#XMLFormat(LocalPlaces[colName][currentRow])#"</cfif></cfloop>/></cfoutput>
	<cfoutput></places></cfoutput>
<cfcatch>
	<cfcontent reset="true">
	<cfoutput><error>#XMLFormat(CFCatch.Message)#</error></cfoutput>
</cfcatch>
</cftry>

<cfsetting enablecfoutputonly="No">